﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class NoticeDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //如果页面第一次加载
        if (!IsPostBack)
        {
            judge();
        }
    }
    /// <summary>
    /// 判断页面传值参数
    /// </summary>
    private void judge()
    {
        if (Request.QueryString["Id"] != null && Request.QueryString["Id"] != "")
        {
            BindWithId(Request.QueryString["Id"]);
        }
    }
    /// <summary>
    /// 增加点击数
    /// </summary>
    /// <param name="id">文章ID</param>
    private void addPageHit(string id)
    {
        string sql = "update Notice_Info set notHits=notHits+1 where notID=" + id;
        DataAccess.ExecuteSQL(sql, null);
    }
    /// <summary>
    /// 绑定上篇文章和下篇文章
    /// </summary>
    protected void upadown()
    {
        int _id4;
        try
        {
            _id4 = Convert.ToInt32(Request.QueryString["Id"].ToString());

        }
        catch
        {
            _id4 = 0;
        }

        string strup = string.Format("select top 1 notTitle,max(notID) as notID from Notice_Info where notID < '{0}' group by notTitle order by notID desc", _id4);
        string strdown = string.Format("select top 1 notTitle , min(notID) as notID from Notice_Info where notID > '{0}' group by notTitle order by notID asc", _id4);
       
        SqlDataReader dr1= DataAccess.GetReader(strup,null);
        if (dr1.Read())
        {
            hyperUp.Text = dr1["notTitle"].ToString().Trim();
            hyperUp.NavigateUrl = "NoticeDetails.aspx?Id=" + dr1["notID"].ToString() + " ";
        }
        else
        {
            hyperUp.Text = "已经到头了!";
        }
        dr1.Close();

        SqlDataReader dr2 = DataAccess.GetReader(strdown, null);
        if (dr2.Read())
        {
            hyperDown.Text = dr2["notTitle"].ToString().Trim();
            hyperDown.NavigateUrl = "NoticeDetails.aspx?Id=" + dr2["notID"].ToString() + " ";
        }
        else
        {
            hyperDown.Text = "已经到了结尾了!";
        }
        dr2.Close();
        
    }
    
    /// <summary>
    /// 显示文章
    /// 2010-11-20
    /// 项玥
    /// </summary>
    /// 2011.4.8修改显示-朱胜贤
    /// <param name="id"></param>
    private void BindWithId(string ID)
    {
        try
        {
            // 添加点击数
            addPageHit(ID);
            //绑定上下篇文章
            upadown();
            string sql = "select notTitle,notContent,notTime,notPublisher,notHits from Notice_Info where notID=" + ID + " and notIsShow=1";
            DataTable dt = DataAccess.GetDataTable(sql, null);
            lblTitleTop.Text = dt.Rows[0]["notTitle"].ToString();
            lblTitle.Text = dt.Rows[0]["notTitle"].ToString();
            lblInfo.Text ="发布人：<span class=\"info_t\">" + dt.Rows[0]["notPublisher"].ToString() + "</span>　发布时间：<span class=\"info_t\">" + dt.Rows[0]["notTime"].ToString() + "</span>　点击数：<span class=\"info_t\">" + dt.Rows[0]["notHits"].ToString() + "</span>";
            lblContent.Text = dt.Rows[0]["notContent"].ToString();
        }
        catch
        {
            lblError.Visible = true;
            lblError.Text = "读取信息失败或信息已删除！！";
        }
    }
    protected void lbtToList_Click(object sender, EventArgs e)
    {
        Response.Redirect("Notice.aspx");
    }
}